//
//  ReplenishingViewController.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/5/12.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "ReplenishingViewController.h"

#import "CustomReplenView.h"

@interface ReplenishingViewController ()<UITextFieldDelegate>
///手机号码
@property (nonatomic,strong) UITextField *phone_TF;
///来电归属地lb
@property (nonatomic,strong) UILabel *callLocationLB;
///分隔线
@property (nonatomic,strong) UIView *lineV;


@end

@implementation ReplenishingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.topTitle = @"手机充值";
    self.topNavView.backgroundColor = MainBackColor;
    self.view.backgroundColor = UIColor.whiteColor;
    
    [self initUI];
    
    //接受支付结果通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payResultClick:) name:@"wechatPayResultNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payResultClick:) name:@"wechatPayResultNotificationSuccess" object:nil];
    
    
}
#pragma mark - noti
-(void)payResultClick:(NSNotification *)noti{
    if (object_getClassName([[Configuration getCurrentVC] class]) != object_getClassName([ReplenishingViewController class])) {
        return;
    }
    NSDictionary *dic = (NSDictionary *)noti.userInfo;
    if ([dic[@"payStatus"] intValue] == 0) {
        [[XNProgressHUD shared] showSuccessWithTitle:@"充值成功！"];
    }else{
        [[XNProgressHUD shared] showErrorWithTitle:dic[@"payResult"]];
    }
}
#pragma mark - UI
-(void)initUI{
    [self.view addSubview:self.phone_TF];
    [self.view addSubview:self.callLocationLB];
    [self.view addSubview:self.lineV];
    
    
    UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(15, self.lineV.bottom+20, 120, 18)];
    lb.text = @"充话费";
    lb.textColor = MainGrayTextColor;
    lb.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:lb];
    
    float btnWidth = (ScreenWidth-20-22)/3;
    float btnHeight = 58;
//    NSArray *arr = @[@"30元",@"50元",@"100元",@"200元",@"300元",@"500元",@"自定义"];
    NSArray *arr = @[@"100元",@"200元"];
    for (int i = 0; i<arr.count; i++) {
        int x = i%3;
        int y = i/3;
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(10 + x * (btnWidth+11), lb.bottom + 11 + y * (btnHeight+10), btnWidth, btnHeight);
        btn.clipsToBounds = YES;
        btn.layer.cornerRadius = 4;
        btn.layer.borderWidth = 1;
        btn.layer.borderColor = MainThemeColor.CGColor;
        [btn.titleLabel setFont:FontSet(17)];
        [btn setTitle:arr[i] forState:normal];
        [btn setTitleColor:MainThemeColor forState:normal];
        btn.tag = i;
        if ([arr[i] isEqualToString:@"自定义"]) {
            [btn addTarget:self action:@selector(customRechargeClick) forControlEvents:UIControlEventTouchUpInside];
        }else{
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        [self.view addSubview:btn];
    }
}

#pragma mark - lazy
- (UITextField *)phone_TF{
    if (!_phone_TF) {
        _phone_TF = [[UITextField alloc] initWithFrame:CGRectMake(15, self.topNavView.bottom+15, ScreenWidth-30, 33)];
        _phone_TF.textAlignment = NSTextAlignmentLeft;
        _phone_TF.font = FontSetWithWeight(24, 0.3f);
        _phone_TF.textColor = MainPureColor(0);
        _phone_TF.keyboardType = UIKeyboardTypeNumberPad;
        _phone_TF.placeholder = @"请输入手机号码";
        _phone_TF.delegate = self;
    }
    return _phone_TF;
}
- (UILabel *)callLocationLB{
    if (!_callLocationLB) {
        _callLocationLB = [[UILabel alloc] initWithFrame:CGRectMake(15, self.phone_TF.bottom, 120, 18)];
        _callLocationLB.font = FontSet(13);
        _callLocationLB.textColor = MainPureColor(0);
        _callLocationLB.textAlignment = NSTextAlignmentLeft;
    }
    return _callLocationLB;
}
- (UIView *)lineV{
    if (!_lineV) {
        _lineV = [[UIView alloc] initWithFrame:CGRectMake(10, self.callLocationLB.bottom+12, ScreenWidth-20, 1)];
        _lineV.backgroundColor = MainPureColor(239);
    }
    return _lineV;
}

#pragma mark - SEL
//自定义充值金额
-(void)customRechargeClick{
    [self.view endEditing:YES];
    if ([Configuration getLoginStatus] == NO) {
        [Configuration loginClick];
        return;
    }
    if (self.phone_TF.text.length != 11) {
        [[XNProgressHUD shared] showWithTitle:@"请输入正确的手机号!"];
        return;
    }
    CustomReplenView *view = [[CustomReplenView alloc] init];
    [view show];
    [view setConfirmReplenBlock:^(NSString * _Nonnull amount) {
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"source"] = @"app";
        params[@"telphone"] = self.phone_TF.text;
        params[@"amount"] = amount;
        [NetWorkingPerson chargeOrderWithParams:params WithSuccessBlock:^(id  _Nonnull responseObj) {
            NSDictionary *payDict = responseObj[@"data"];
            [WechatPayManager handleWechatPayWithPayDic:payDict];
        }];
    }];
}
-(void)btnClick:(UIButton *)btn{
    [self.view endEditing:YES];
    if ([Configuration getLoginStatus] == NO) {
        [Configuration loginClick];
        return;
    }
    if (self.phone_TF.text.length != 11) {
        [[XNProgressHUD shared] showWithTitle:@"请输入正确的手机号!"];
        return;
    }
    NSInteger index = (NSInteger)btn.tag;
    NSArray *arr = @[@(100),@(200)];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"source"] = @"app";
    params[@"telphone"] = self.phone_TF.text;
    params[@"amount"] = arr[index];
    [NetWorkingPerson chargeOrderWithParams:params WithSuccessBlock:^(id  _Nonnull responseObj) {
        NSDictionary *payDict = responseObj[@"data"];
        [WechatPayManager handleWechatPayWithPayDic:payDict];
    }];
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidChangeSelection:(UITextField *)textField{
    self.callLocationLB.text = [self getNumberAttribution:textField.text];
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    self.callLocationLB.text = [self getNumberAttribution:textField.text];
}
//获取号码归属地
-(NSString *)getNumberAttribution:(NSString *)numer{
    NSString *CM = @"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$)|(^1705\\d{7}$)";
    NSString *CU = @"(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}$)|(^1709\\d{7}$)";
    NSString *CT = @"(^1(33|53|77|8[019])\\d{8}$)|(^1700\\d{7}$)";
    if ([[NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM] evaluateWithObject:numer]) {
        return @"中国移动";
    }
    if ([[NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU] evaluateWithObject:numer]) {
        return @"中国联通";
    }
    if ([[NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT] evaluateWithObject:numer]) {
        return @"中国电信";
    }
    return @"";
}


@end
